import login from './login.js'
import loginToutiao from './login_toutiao.js'
import loginAlipay from './login_alipay.js'
// 页面跳转 判断是否登录
const toPath = function(url, obj, isButton = true) {
	let checkLogin = obj.$store.state.hasLogin;
	if (checkLogin) {
		uni.navigateTo({
			url: url
		})
	} else {

		// #ifdef APP-PLUS
		uni.navigateTo({
			url: '/platforms/app-plus/login/phone'
		});
		// #endif

		// #ifdef H5
		uni.navigateTo({
			url: '/platforms/h5/login/phone'
		});
		// #endif

		// #ifdef MP-WEIXIN

		if (isButton) {
			// login(obj, url);
			uni.setStorageSync('toUrl', url);
			uni.navigateTo({
				url: '/platforms/mp-weixin/login/login'
			})
		} else {
			uni.showToast({
				title: '请先登录',
				icon: 'none'

			})
			uni.switchTab({
				url: '/pages/ucenter/index'
			})
		}

		// #endif

		// #ifdef MP-TOUTIAO
		if (isButton) {
			loginToutiao(obj, url);
		} else {
			uni.showToast({
				title: '请先登录',
				icon: 'none'

			})
			uni.switchTab({
				url: '/pages/ucenter/index'
			})
		}
		// #endif

		// #ifdef MP-ALIPAY
		if (isButton) {
			console.log(obj, url);
			loginAlipay(obj, url);
		} else {
			uni.showToast({
				title: '请先登录',
				icon: 'none'

			})
			uni.switchTab({
				url: '/pages/ucenter/index'
			})
		}
		// #endif
	}
}

/** 页面路由管理 **/
/**
 * 订单详情页 页面路由
 * @param {Number} typeid 产品类型id
 * @param {Object} params 传递的参数值 以键值方式放在请求地址中
 */
const orderDetailPage = {
	1: '/subPackages/lines/orderDetail',
	2: '/subPackages/hotels/orderDetail',
	3: '/subPackages/cars/orderDetail',
	5: '/subPackages/spots/orderDetail',
	888: '/subPackages/ferrys/orderDetail',
	8: '/subPackages/visa/orderDetail',
	13: '/subPackages/tuan/orderDetail',
	14: '/subPackages/customize/orderDetail',
	21: '/subPackages/malls/orderDetail',
	23: '/subPackages/combination/orderDetail',
	104: '/subPackages/ships/orderDetail',
	106: '/subPackages/guide/orderDetail',
	107: '/subPackages/club/orderDetail',
	114: '/subPackages/outdoors/orderDetail',
	118: '/subPackages/techan/orderDetail',
	196: '/subPackages/wallet/orderDetail',
	198: '/subPackages/memberCard/orderDetail',
	121: '/subPackages/foods/orderDetail',
};
const getOrderDetailPage = function(typeid, params = {}) {
	let res = '';
	let tongyong_typeid = uni.getStorageSync('uniTongYongTypeId');
	if (typeid) {
		if (orderDetailPage[typeid]) {
			res = orderDetailPage[typeid] + '?';
			// 拼接参数
			let prefix = '';
			for (let i in params) {
				res += prefix + i + '=' + params[i];
				prefix = '&';
			}
		} else if (tongyong_typeid[typeid]) { //判断是否是通用typeid
			let tongyong_link = '/subPackages/tongyong/orderDetail?';
			let prefix = '';
			for (let i in params) {
				tongyong_link += prefix + i + '=' + params[i];
				prefix = '&';
			}
			res = tongyong_link;
		} else {
			uni.showToast({
				title: '应用未开通',
				icon: 'none'
			})
		}
	}

	return res;
}

/**
 * 检查是否有订单页面
 * @param {Number} typeid
 * @return {Boolean} true|false
 */
const hasOrderDetailPage = function(typeid) {
	let res = '';
	let tongyong_typeid = uni.getStorageSync('uniTongYongTypeId');
	if (typeid) {
		if (orderDetailPage[typeid]) {
			return true;
		} else if (tongyong_typeid[typeid]) { //判断是否是通用typeid
			return true;
		}
	}
	return false;
}

/**
 * 产品首页 页面路由
 * @param {Number} typeid 产品类型id
 * @param {Object} params 传递的参数值 以键值方式放在请求地址中
 */
const proIndexPage = {
	1: '/subPackages/lines/index',
	2: '/subPackages/hotels/index',
	3: '/subPackages/cars/index',
	4: '/subPackages/strategy/index',
	5: '/subPackages/spots/index',
	888: '/subPackages/ferrys/index',
	8: '/subPackages/visa/index',
	13: '/subPackages/tuan/index',
	21: '/subPackages/malls/index',
	23: '/subPackages/combination/index',
	104: '/subPackages/ships/index',
	101: '/subPackages/note/index',
	106: '/subPackages/guide/index',
	107: '/subPackages/club/mall',
	114: '/subPackages/outdoors/index',
	115: '/subPackages/news/list',
	118: '/subPackages/techan/index',
	198: '/subPackages/memberCard/list',
	121: '/subPackages/foods/index',
};
const getProIndexPage = function(typeid) {
	let res = '';
	if (typeid) {
		if (typeid > 199) {
			res = '/subPackages/tongyong/list?typeid=' + typeid;
		} else {
			if (proIndexPage[typeid]) {
				res = proIndexPage[typeid];
			} else {
				uni.showToast({
					title: '应用未开通',
					icon: 'none'
				})
			}
		}
	} else {
		uni.showToast({
			title: '应用未开通',
			icon: 'none'
		})
	}
	return res;
}

/**
 * 产品列表 页面路由
 * @param {Number} typeid 产品类型id
 * @param {Object} params 传递的参数值 以键值方式放在请求地址中
 */
const proListPage = {
	1: '/subPackages/lines/list',
	2: '/subPackages/hotels/list',
	3: '/subPackages/cars/list',
	4: '/subPackages/strategy/list',
	5: '/subPackages/spots/list',
	888: '/subPackages/ferrys/list',
	8: '/subPackages/visa/list',
	13: '/subPackages/tuan/list',
	21: '/subPackages/malls/list',
	23: '/subPackages/combination/list',
	101: '/subPackages/note/index',
	104: '/subPackages/ships/lineList',
	106: '/subPackages/guide/list',
	107: '/subPackages/club/list',
	114: '/subPackages/outdoors/list',
	115: '/subPackages/news/list',
	118: '/subPackages/techan/list',
	198: '/subPackages/memberCard/list',
	121: '/subPackages/foods/list',
};
const getProListPage = function(typeid) {
	let res = '';
	if (typeid) {
		if (typeid > 199 && typeid != 888) {
			res = '/subPackages/tongyong/list?typeid=' + typeid;
		} else {
			if (proListPage[typeid]) {
				res = proListPage[typeid];
			} else {
				uni.showToast({
					title: '应用未开通',
					icon: 'none'
				})
			}
		}
	} else {
		uni.showToast({
			title: '应用未开通',
			icon: 'none'
		})
	}
	return res;
}

/**
 * 产品详情页 页面路由
 * @param {Number} typeid 产品类型id
 * @param {Object} params 传递的参数值 以键值方式放在请求地址中
 */
const proDetailPage = {
	1: '/subPackages/lines/detail',
	2: '/subPackages/hotels/detail',
	3: '/subPackages/cars/detail',
	4: '/subPackages/strategy/detail',
	5: '/subPackages/spots/detail',
	888: '/subPackages/ferrys/detail',
	6: '/subPackages/album/detail',
	8: '/subPackages/visa/detail',
	13: '/subPackages/tuan/detail',
	21: '/subPackages/malls/detail',
	23: '/subPackages/combination/detail',
	101: '/subPackages/note/detail',
	104: '/subPackages/ships/lineDetail',
	106: '/subPackages/guide/detail',
	107: '/subPackages/club/detail',
	114: '/subPackages/outdoors/detail',
	115: '/subPackages/news/detail',
	118: '/subPackages/techan/detail',
	198: '/subPackages/memberCard/detail',
	121: '/subPackages/foods/detail',
};
const getProDetailPage = function(typeid, params = {}) {
	let res = '';
	let tongyong_typeid = uni.getStorageSync('uniTongYongTypeId');
	if (typeid) {
		if (proDetailPage[typeid]) {
			res = proDetailPage[typeid] + '?';
			// 拼接参数
			let prefix = '';
			for (let i in params) {
				res += prefix + i + '=' + params[i];
				prefix = '&';
			}
		} else if (tongyong_typeid[typeid]) { //判断是否是通用typeid
			let tongyong_link = '/subPackages/tongyong/detail?t=' + typeid;
			let prefix = '&';
			for (let i in params) {
				tongyong_link += prefix + i + '=' + params[i];
			}
			res = tongyong_link;
		} else {
			uni.showToast({
				title: '应用未开通',
				icon: 'none'
			})
		}
	}

	return res;
}

/**
 * 产品预订页 页面路由
 * @param {Number} typeid 产品类型id
 * @param {Object} params 传递的参数值 以键值方式放在请求地址中
 */
const proBookingPage = {
	1: '/subPackages/lines/booking',
	2: '/subPackages/hotels/booking',
	3: '/subPackages/cars/booking',
	5: '/subPackages/spots/booking',
	888: '/subPackages/ferrys/booking',
	8: '/subPackages/visa/booking',
	13: '/subPackages/tuan/booking',
	21: '/subPackages/malls/booking',
	23: '/subPackages/combination/booking',
	104: '/subPackages/ship/booking',
	107: '/subPackages/club/booking',
	114: '/subPackages/outdoors/booking',
	118: '/subPackages/techan/booking',
	121: '/subPackages/foods/booking',
};
const getProBookingPage = function(typeid, params = {}) {
	let res = '';
	if (typeid) {
		if (proBookingPage[typeid]) {
			res = proBookingPage[typeid] + '?';
			// 拼接参数
			let prefix = '';
			for (let i in params) {
				res += prefix + i + '=' + params[i];
				prefix = '&';
			}
		} else {
			uni.showToast({
				title: '应用未开通',
				icon: 'none'
			})
		}
	}

	return res;
}


/** 页面路由管理 **/

/** 模块名称管理 **/
/**
 * @param {Object} typeid 产品模块id
 */
const proNavTitle = function(typeid) {
	let uniappModelData = uni.getStorageSync('uniappModelData');
	if (uniappModelData && uniappModelData[typeid]) {
		return uniappModelData[typeid].title;
	}
	return '';
}
/** 模块名称管理 **/

// 文件下载
const downloadFile = function(filePath, fileExt = '') {
	uni.showLoading({
		title: '下载中'
	});
	let fileExtension = fileExt == '' ? filePath.substring(filePath.lastIndexOf('.') + 1) : fileExt;
	const downloadTask = uni.downloadFile({
		url: filePath, //仅为示例，并非真实的资源
		success: (res) => {
			if (res.statusCode === 200) {
				let tempFilePath = res.tempFilePath;
				uni.saveFile({
					tempFilePath: tempFilePath,
					success: function(res) {
						let savedFilePath = res.savedFilePath;

						uni.showToast({
							title: '文件下载成功,打开中',
							icon: 'none'
						});
						setTimeout(() => {
							uni.openDocument({
								filePath: savedFilePath,
								fileType: fileExtension,
								success: function(res) {

								},
								fail(err) {
									uni.showToast({
										title: '不支持打开该类型的文件',
										icon: 'none'
									});
								}
							});
						}, 1500)
					},
					fail(err) {
						uni.showToast({
							title: '文件保存失败',
							icon: 'none'
						});
					}
				});

			} else {
				uni.showToast({
					title: '文件下载失败',
					icon: 'none'
				});
			}
		},
		fail(err) {
			uni.showToast({
				title: '文件为空，下载失败',
				icon: 'none'
			});
		},
		complete() {
			uni.hideLoading();
		}
	});

	downloadTask.onProgressUpdate((res) => {

	});
}

// 前往系统首页
const toAppIndex = function() {
	uni.switchTab({
		url: "/pages/index/home"
	})
}

export {
	toPath,
	downloadFile, // 文件下载
	getOrderDetailPage, // 订单详情页 页面路由
	getProIndexPage, // 产品模块首页
	getProListPage, // 产品模块列表页
	getProDetailPage, // 产品详情页路由
	getProBookingPage, //产品预订页路由
	proNavTitle, // 模块名称管理 使用typeid获取
	toAppIndex, // 前往系统首页
	hasOrderDetailPage, // 是否有订单详情页面
}